﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace HmLaunch;

internal class LaunchMainRunner {

  public void Run() {
    (string exePath, string func) = GetAppExe();

    var PowerSh = (string cmd) => {
      Process proc = new();
      ProcessStartInfo startCfg = proc.StartInfo;
      startCfg.WorkingDirectory = Path.GetDirectoryName(exePath);

      startCfg.UseShellExecute = false;
      startCfg.FileName = "powershell.exe";
      startCfg.ArgumentList.Add("-Command");
      startCfg.ArgumentList.Add(cmd);

      proc.Start();
      return proc;
    };

    string exeCmd = $".\\{Path.GetFileName(exePath)}";
    string fullCmd = $"{exeCmd} {func} prod";
    Console.WriteLine(fullCmd);
    //PowerSh("pause").WaitForExit();
    //return;

    Process proc = PowerSh(fullCmd);
    proc.WaitForExit();

    if (proc.ExitCode != 0) {
      Console.WriteLine("--------------- " + proc.ExitCode);
      PowerSh("pause").WaitForExit();
      return;
    }

    Thread.Sleep(1000);
  }

  (string, string) GetAppExe() {
    string exePath = Path.GetFullPath(@"..\app\haimen.exe");
    if (!File.Exists(exePath)) {
      //string devExe = @"E:\work_etc\nina\2023haimen2\workshop_cs\haimen\bin\Debug\net8.0-windows\haimen.exe";
      string devExe = @"D:\work\personal\nina\2023haimen2\workshop_z\bin\app\haimen.exe";
      return (devExe, "功能_2");
    }

    string workingDir = Path.GetFullPath(".");
    string func = Path.GetFileName(workingDir);
    return (exePath, func);
  }
}
